<template>
  <div class="siderbar-inner" :class="{'close':getSystemConfig.isCollapse}" v-loading="getIsLoading">
    <!-- 用户信息 -->
    <div class="user-detail">
      <!-- 用户头像 -->
      <div class="user-thumb">
        <img :src="getUserInfo.avatar" :alt="getUserInfo.account" :title="getUserInfo.account" />
      </div>
      <!-- <div class="user-info"></div> -->
      <div class="user-info">
        <el-dropdown>
          <span class="el-dropdown-link">
            <span class="account">
              {{getUserInfo.account}}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <span class="role">( {{getUserInfo.role}} )</span>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>
              <router-link to="/dashboard/personal/profile">个人信息</router-link>
            </el-dropdown-item>
            <el-dropdown-item>
              <router-link to="/dashboard/personal/history">登录记录</router-link>
            </el-dropdown-item>
            <el-dropdown-item @click.native="logoutEvent()">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <!-- 导航 -->
    <div class="siderbar-nav">
      <el-menu
        default-active="1-4-1"
        class="el-menu-vertical-demo"
        :collapse="getSystemConfig.isCollapse"
        :backgroundColor="getSystemConfig.theme"
        :unique-opened="true"
        :router="true"
      >
        <el-menu-item index="/">
           <i class="el-icon-s-home"></i>
            <span slot="title">后台首页</span>
        </el-menu-item>
        <el-submenu :index="v.path" v-for="(v,k) in getHomeNavData " :key="k">
          <template slot="title">
            <i :class="v.icon"></i>
            <span slot="title">{{v.title}}</span>
          </template>
          <el-menu-item-group>
            <el-menu-item v-for="(item,key) in v.children" :key="key">
              <router-link :to="'/dashboard/'+v.path+'/'+item.path">{{item.title}}</router-link>
               <!-- <span slot="title">{{item.name}}</span> -->
            </el-menu-item>
          </el-menu-item-group>
        </el-submenu>
      </el-menu>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
export default {
  name: "SiderBar",
  data() {
    return {};
  },
  computed: {
    ...mapGetters("user", ["getUserInfo"]),
    ...mapGetters("system", ["getSystemConfig", "getHomeNavData",'getIsLoading'])
  },
  methods: {
    ...mapActions("user", ["logout"]),
    ...mapActions('system',['clearRoutes']),
    logoutEvent() {
      //  console.log('正在退出...');
      this.$http.logout().then(res => {
        if (res && res.data.code == 200) {
          //退出登录成功 (服务器已经将当前的令牌销毁)
          //  1. 将本地的token 删除 2.跳转到登录页
          this.logout().then(() => {
            this.$message({
              type: "success",
              message: "您已经成功退出登录！"
            });
            //清空本地用户的路由等系统配置信息
            this.clearRoutes();
            //重新定向到登录页
            this.$router.push("/login");
            setTimeout(function(){
              window.location.reload();
            },100)
          });
        }
      });
    }
  }
};
</script>

<style scoped lang="less">
.siderbar-inner {
  width: 200px;
  height: 100vh;
  transition: width 0.35s;
  .user-detail {
    .user-thumb {
      width: 80px;
      height: 80px;
      background: #ccc;
      margin: 10px auto;
      border-radius: 50%;
      overflow: hidden;
      box-shadow: 0 0 0px 3px rgba(255, 255, 255, 0.5);
      cursor: pointer;
      img {
        display: block;
        width: 100%;
        height: 100%;
      }
    }
    .user-info {
      text-align: center;
      font-weight: 600;
      padding: 0 3px;
      white-space: nowrap;
      text-overflow: ellipsis;
      .account {
        font-size: 18px;
        text-transform: capitalize;
      }
      .role {
        display: block;
        font-size: 12px;
        line-height: 30px;
        color: #ccc;
      }

      overflow: hidden;
      .el-dropdown {
        color: #f1f1f1;
      }
    }
    &::after {
      content: "";
      display: block;
      width: 80%;
      height: 0.5px;
      margin: 0px auto 10px auto;
      background-color: rgba(255, 255, 255, 0.2);
    }
  }

  .siderbar-nav {
    height: calc(100vh - 160px);
    overflow: auto;
    padding-bottom: 20px;
  }
  .el-menu {
    border: none;
    .el-menu-item {
      height: 40px;
      line-height: 40px;
      a ,span{
        color: #ccc;
        font-weight: bold;
      }
    }

    /deep/.el-submenu__title {
      height: 40px;
      line-height: 40px;
      span {
        color: #ccc;
        font-weight: bold;
      }
      i {
        color: #ccc;
      }
    }
    .el-menu-item-group {
      li {
        text-indent: 20px;
      }
    }
    .el-submenu {
      .el-submenu__title {
        height: 40px;
        line-height: 40px;
      }
      .el-menu-item {
        height: 40px;
        line-height: 40px;
        a {
          display: block;
          color: #ccc;
          font-weight: bold;
        }
      }
      /deep/.el-menu-item-group__title {
        padding: 0;
      }
    }
  }
}

.siderbar-inner.close {
  width: 64px;
  .user-detail {
    position: relative;
    .user-thumb {
      width: 50px;
      height: 50px;
      margin: 10px auto;
      box-shadow: 0 0 0px 2px rgba(255, 255, 255, 0.5);
    }
    .user-info {
      .el-dropdown {
        position: absolute;
        top: 0px;
        left: 0;
        width: 100%;
        cursor: pointer;
        .el-dropdown-link {
          display: block;
          height: 50px;
        }
      }
      .account {
        font-size: 0px;
      }
      .role {
        font-size: 0px;
      }

      overflow: hidden;
      .el-dropdown {
        color: #f1f1f1;
      }
    }
    &::after {
      width: 70%;
    }
  }
}

.el-dropdown-menu__item a {
  color: #666;
}
</style>